package com.supermap.tool;

import com.alibaba.fastjson.JSONObject;
import com.supermap.dubbo.DataDubboConcumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author yuyao
 * @ClassName: TableToo
 * @Description: 表格数据工具
 * @date 2018年3月23日 上午9:29:03
 */
@Service
public class TableTool {

    @Autowired
    DaoTool daoTool;
    @Resource
    private DataDubboConcumerService dataDubboConcumerService;

    /**
     * @param @param  table
     * @param @param  val
     * @param @param  con
     * @param @param  tableName
     * @param @return 设定文件
     * @return TableVoo    返回类型
     * @throws
     * @Title: getTable
     * @Description: 表格封装工具
     */
    public TableVoo getTable(TableVoo table, String val, String con, String tableName) {
        int ks = table.getLimit() * (table.getPage() - 1);
        //计算起始位置
        if (ks < 0) {
            ks = 0;
        }
        try {
            MysqlEntity st = new MysqlEntity();
            st.setTableName(tableName);
            st.setSql(val + " LIMIT " + ks + "," + table.getLimit() + ";");
            JSONObject responseData = dataDubboConcumerService.getDataWithSelect(val + " LIMIT " + ks + "," + table.getLimit() + ";");
            if (responseData.getInteger("code") == 200) {
                table.setData(responseData.getJSONArray("data"));
            }

            st.setSql(con);

            responseData = dataDubboConcumerService.getDataWithSelect(con);
            if (responseData.getInteger("code") == 200 && responseData.get("data") != null) {
                table.setCount(responseData.getJSONArray("data").getJSONObject(0).getInteger("count"));
            }

        } catch (Exception e) {
            System.out.println("表格数据封装异常！" + e.getMessage());
            return table;
        }
        return table;
    }
}
